﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace GameServer.Managers
{
    struct MapInfo
    {
        private int _ID;
        private string _Name;

        private int _Flags;

        public int ID { get { return _ID; } }
        public string Name { get { return _Name; } }

        public MapInfo(int iID, string sName, int iFlags)
        {
            this._ID = iID;
            this._Name = sName;
            this._Flags = iFlags;
        }
    }

    class MapManager
    {
        private static Hashtable _MapList = new Hashtable();

        public static bool addMap(int id, MapInfo info)
        {
            if (_MapList.ContainsKey(id) == false)
            {
                _MapList.Add(id, info);
                return true;
            }
            else
            {
                Log.WriteError("Duplicate Map [" + info.ID+ "-" + info.Name + "] in the table!");
            }

            return false;
        }

        public static bool load()
        {
            try
            {
                int[] MapIDs = DB.runReadColumn("SELECT id FROM Maps", 0, null);
                for (int I = 0; I < MapIDs.Length; I++)
                {
                    string[] MapData = DB.runReadRow("SELECT name, flags FROM Maps WHERE id='" + MapIDs[I] + "'");
                    MapInfo _Map = new MapInfo(MapIDs[I], MapData[0], int.Parse(MapData[1]));
                    addMap(MapIDs[I], _Map);
                }
                return true;
            }
            catch { }
            return false;
        }

        public static MapInfo getMap(int ID)
        {
            if (_MapList.ContainsKey(ID))
                return (MapInfo)_MapList[ID];

            return new MapInfo(-1, "", 2);
        }
    }
}
